﻿using BaseUi.Views.MainWindows;
using HandyControl.Controls;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;

namespace BaseUi.Views.PlcView
{
    public class JawData : INotifyPropertyChanged
    {
        private SolidColorBrush startColor = new SolidColorBrush();
        private SolidColorBrush newColor = new SolidColorBrush();
        private string jiazhuwz = "";
        public SolidColorBrush start
        {
            get { return startColor; }
            set
            {
                startColor = value;

                OnPropertyChanged("start");
            }
        }
        public SolidColorBrush neowstation
        {
            get { return newColor; }
            set
            {
                newColor = value;

                OnPropertyChanged("neowstation");
            }
        }
        public string jiazhu
        {
            get { return jiazhuwz; }
            set
            {
                jiazhuwz = value;

                OnPropertyChanged("jiazhu");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    /// <summary>
    /// JawView.xaml 的交互逻辑
    /// </summary>
    public partial class JawView : UserControlBase
    {
        private JawData jawModel = new JawData();
        public SolidColorBrush RedColor = new SolidColorBrush(Color.FromRgb(255, 69, 0));
        public SolidColorBrush GreenColor = new SolidColorBrush(Color.FromRgb(173, 255, 47));
        public SolidColorBrush BoroColor = new SolidColorBrush(Color.FromRgb(220, 220, 220));
        public SolidColorBrush LemonChiffonColor = new SolidColorBrush(Color.FromRgb(255, 250, 205));
        public JawView()
        {
            InitializeComponent();
            DataContext = jawModel;
        }

        private void loadFirst(object sender, EventArgs e)
        {
            if (this.IsInDesignMode)
                return;
            Getnumber();
            Task Jawzhuant = new Task(() =>
            {
                while (true)
                {
                    getnewtim();
                    Thread.Sleep(200);
                }
            });
            Jawzhuant.Start();
        }
        private void getnewtim()
        {
            var ioi = LocalStatic.Plc.Read<bool>("201");
            if (ioi.IsSucceed==false)
            {
                jawModel.start = RedColor;
            }
            else
            {
                if (ioi.Value)
                {
                    jawModel.start = LemonChiffonColor;
                }
                else
                {
                    jawModel.start = BoroColor;
                }
            }
            ioi = LocalStatic.Plc.Read<bool>("204");
            if (ioi.IsSucceed == false)
            {
                jawModel.neowstation = RedColor;
            }
            else
            {
                if (ioi.Value)
                {
                    jawModel.neowstation = LemonChiffonColor;
                }
                else
                {
                    jawModel.neowstation = BoroColor;
                }
            }
            var ioi1 = LocalStatic.Plc.Read<float>("22");
            if (ioi1.IsSucceed == false)
            {
                jawModel.jiazhu = "未接收";
            }
            else
            {
                jawModel.jiazhu = ioi1.Value.ToString();
            }
        }
        
        private void Getnumber()
        {
            var ioi = LocalStatic.Plc.Read<float>("2002");
            if (ioi.IsSucceed==false)
            {
                JawBoxMoCode1g1.Text ="未接收";
            }else
            {
                JawBoxMoCode1g1.Text = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("2004");
            if (ioi.IsSucceed == false)
            {
                JawBoxMoCode1g2.Text = "未接收";
            }
            else
            {
                JawBoxMoCode1g2.Text = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("2000");
            if (ioi.IsSucceed == false)
            {
                JawBoxMoCode1g3.Text = "未接收";
            }
            else
            {
                JawBoxMoCode1g3.Text = ioi.Value.ToString();
            }
            //float dw;
            //dw = LocalStatic.Plc.Read<float>("2002").Value;
            //JawBoxMoCode1g1.Text = dw.ToString();
            //dw = LocalStatic.Plc.Read<float>("2004").Value;
            //JawBoxMoCode1g2.Text = dw.ToString();
            //dw = LocalStatic.Plc.Read<float>("2000").Value;
            //JawBoxMoCode1g3.Text = dw.ToString();
        }

        private void outboxpButton_Click2g1z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("130", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void outboxpButton_Click2g1z2(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("131", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void outboxpButton_Click2g2z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("107", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void outboxpButton_Click2g3z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("108", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void outboxpButton_Click2g4z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("109", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }


        private void outboxpButton_Click1g1z1(object sender, RoutedEventArgs e)
        {
            Getnumber();
        }
        /// <summary>
        /// 夹紧位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_MouseDoubleClick1g1(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(JawBoxMoCode1g1.Text);
                var ioi = LocalStatic.Plc.Write<float>("2002", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 张开位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_MouseDoubleClick1g2(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(JawBoxMoCode1g2.Text);
                var ioi = LocalStatic.Plc.Write<float>("2004", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 定位误差
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_MouseDoubleClick1g3(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(JawBoxMoCode1g3.Text);
                var ioi = LocalStatic.Plc.Write<float>("2000", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
    }
}
